package logic

import (
	"context"
	"three-sxp/goodsrpc/goodsrpcclient"

	"three-sxp/api/internal/svc"
	"three-sxp/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchEsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSearchEsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchEsLogic {
	return &SearchEsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SearchEsLogic) SearchEs(req *types.SearchEsRequest) (resp *types.SearchEsResponse, err error) {
	// todo: add your logic here and delete this line
	res, _ := l.svcCtx.GoodsRpc.SearchEs(l.ctx, &goodsrpcclient.SearchEsRequest{
		Title: req.Title,
	})
	if res.Code != 200 {
		return &types.SearchEsResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
		}, nil
	}
	var data map[int]interface{}
	for _, v := range res.Data {
		data = map[int]interface{}{
			int(v.Id): map[string]interface{}{
				"GoodsName":    v.GoodsName,
				"GoodsPrice":   v.GoodsPrice,
				"GoodsStock":   v.GoodsStock,
				"GoodsDetails": v.GoodsDetails,
			},
		}
	}
	return &types.SearchEsResponse{
		Code: int(res.Code),
		Msg:  res.Msg,
		Data: data,
	}, nil
}
